package ctgu.yao.cloud.controller;


import ctgu.yao.cloud.dto.PageDTO;
import ctgu.yao.cloud.dto.ResultDTO;
import ctgu.yao.cloud.form.AdminForm;
import ctgu.yao.cloud.form.RuleForm;
import ctgu.yao.cloud.po.Admin;
import ctgu.yao.cloud.service.AdminService;
import ctgu.yao.cloud.service.CompanyuserService;
import ctgu.yao.cloud.util.ResultDTOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Y
 * @since 2022-08-16
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private CompanyuserService companyuserService;
    //登录
    @GetMapping("/login")
    public ResultDTO login(RuleForm ruleForm){
        ResultDTO resultDTO =this.adminService.login(ruleForm);
        return resultDTO;
    }

    @GetMapping("/findById/{mid}")
    public ResultDTO findByID(@PathVariable("mid") Integer mid){
        Admin byId = this.adminService.getById(mid);
        return ResultDTOUtil.success(byId);
    }
    @PutMapping("/update")
    public  ResultDTO  update(@RequestBody  Admin admin){
        boolean update = this.adminService.updateById(admin);
        if(!update) return ResultDTOUtil.fail();
        else  return ResultDTOUtil.success(null);

    }
    @GetMapping("/list/{page}/{size}/{mid}")
    public ResultDTO list(@PathVariable("page") Integer page,@PathVariable("size") Integer size ,@PathVariable("mid") Integer mid){
        PageDTO pageDTO= this.adminService.list(page, size, mid);
        return  ResultDTOUtil.success(pageDTO);
    }
    @GetMapping("/search")
    public  ResultDTO search(AdminForm adminForm){

        PageDTO pageDTO= this.adminService.search(adminForm);
        return  ResultDTOUtil.success(pageDTO);
    }
    @DeleteMapping("/deleteById/{cuid}")
    public ResultDTO  deleteById(@PathVariable("cuid") Integer cuid){
        ResultDTO resultDTO = this.adminService.ComDelete(cuid);
        return resultDTO;
    }


}
